﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using LERP.Domain;
using LERP.Domain.Enums;
using Sharp.Framework.Queries;

namespace LERP.Web.App_Start
{
    public static class ActionLinkExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="text"></param>
        /// <param name="action"></param>
        /// <param name="controller"></param>
        /// <param name="routeValues"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkEx(this HtmlHelper html, string text, string action, string controller, object routeValues, object htmlAttributes)
        {
            var userId = int.Parse(html.ViewContext.HttpContext.User.Identity.Name);
            var isAllowed = IsAllowed(userId, controller, action, "all");

            if (!isAllowed)
            {
                return new MvcHtmlString("");
            }
            return html.ActionLink(text, action, controller,routeValues,htmlAttributes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="text"></param>
        /// <param name="action"></param>
        /// <param name="controller"></param>
        /// <param name="routeValues"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkEx(this HtmlHelper html, string text, string action, string controller, RouteValueDictionary routeValues, Dictionary<string, object> htmlAttributes)
        {
            var userId = int.Parse(html.ViewContext.HttpContext.User.Identity.Name);
            var isAllowed = IsAllowed(userId, controller, action, "all");

            if (!isAllowed)
            {
                return new MvcHtmlString("");
            }
            return html.ActionLink(text, action, controller, routeValues, htmlAttributes);
        }
        private static bool IsAllowed(int userid, string controller, string action, string method)
        {
            var allActions = FunctionConfigManger.AllActions;
            if (allActions.Any(
                m => m.Contrller.ToLower() == controller.ToLower() && m.Name.ToLower() == action.ToLower() &&
                     (m.Method == "all" || m.Method.ToLower() == method.ToLower())))
            {
                var query = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<IQuery>();
                var user = query.Get<User>(userid);
                if (user.Role != UserRole.System)
                {
                    var functionId = user.Functions;

                    var actions = FunctionConfigManger.GetActions(functionId);
                    return
                        actions.Any(
                            m =>
                            m.Contrller.ToLower() == controller.ToLower() && m.Name.ToLower() == action.ToLower() &&
                            (m.Method == "all" || m.Method.ToLower() == method.ToLower()));

                }
            }
            return true;
        }
    }
}